let express=require('express')
let app=express()
let cors=require('cors')
const joi = require('@hapi/joi')
const config = require('./config')
const expressJWT = require('express-jwt')
const userRouter= require('./router/user.js')
const userinfoRouter = require('./router/userinfo')
const artCateRouter = require('./router/artcate')
const articleRouter = require('./router/article')
app.use(cors())
app.use(express.urlencoded({
	extended:false
}))

	app.use(function (req, res, next) {
		res.cc = function (err, status = 1) {
		res.send({
		// 状态
		status,
		// 状态描述，判断 err 是 错误对象 还是 字符串
		message: err instanceof Error ? err.message : err,
		})
		}
		next()
		})
		app.use(expressJWT({ secret: config.jwtSecretKey }).unless({ path: [/^\/api\//] }))

		

		app.use(function (err, req, res, next) {
		//验证失败
		if (err instanceof joi.ValidationError) return res.cc(err)
		// 捕获身份认证失败的错误
		if (err.name === 'UnauthorizedError') return res.cc('身份认证失败！')
		res.cc(err)
		})

app.use('/article', artCateRouter)app.use('/api',userRouter)
app.use('/my', userinfoRouter)
app.use('/my/article', articleRouter)
app.use('/uploads', express.static('./uploads'))
app.listen(3007,function(){
	console.log('http://127.0.0.1:3007')
})